package ${initProjectVo.packageName}.utils;

import ${initProjectVo.packageName}.entity.UserInfo;
import ${initProjectVo.packageNameValue}.common.constant.Constants;
import ${initProjectVo.packageNameValue}.common.jwt.JWTUtils;
import ${initProjectVo.packageNameValue}.common.lang.JsonUtils;
import ${initProjectVo.packageNameValue}.common.redis.JRedisUtils;

import javax.servlet.http.HttpServletRequest;
/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2021/4/8 10:15
 * @Description
 */
public class SessionUtils {
    /**
    * 获取用户登录名称
    * @param sessionId
    * @return
    */
    public static String getSessionUserName(String sessionId){
        UserInfo myUserDetails =  JsonUtils.toJsonBean(JRedisUtils.getKeyValue(Constants.USER_OAUTH_SESSION_ID+sessionId),UserInfo.class);
        return myUserDetails.getUsername();
    }

    /**
    * 获取用户登录ID
    * @param sessionId
    * @return
    */
    public static Integer getSessionUserId(String sessionId){
        UserInfo myUserDetails =  JsonUtils.toJsonBean(JRedisUtils.getKeyValue(Constants.USER_OAUTH_SESSION_ID+sessionId),UserInfo.class);
        return myUserDetails.getId();
    }



    /**
    * base on toke
    * @param request
    * @return
    */
    public static String  getTokenForUserSessionId(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.AUTH_HEADER_KEY);
        String sessionId =  JWTUtils.getTokenObjectValue(token,JWTUtils.SESSION_ID_KEY);
        return sessionId;
    }
}
